<?php

namespace Home\Controller;


class IndexController extends CommonController
{
    /**
     * 前端首页
     */
    public function index()
    {
//        dump($_SESSION);  //打印微信授权用户信息
        //查出所有置顶商品
        $product_tops = $this->product->where("status = 1 and is_top = 1")->limit(3)->select();
        //查出所有新品
        $product_news = $this->product->where("status = 1 and is_new = 1")->limit(2)->select();
        //查出所有热销商品
        $product_hots = $this->product->where("status = 1 and is_hot = 1")->limit(2)->select();
        //查出所有推荐商品
        $product_recommends = $this->product->where("status = 1 and is_recommend = 1")->limit(2)->select();

        $this->assign(compact('product_tops', 'product_news', 'product_hots', 'product_recommends'));
        $this->display();
    }

    /**
     * 查看所有商品
     */
    public function all()
    {
        $total = $this->product->where("status = 1")->count();
        $products = $this->product->where("status = 1")->limit(2)->select();

        $this->assign(compact('products', 'total'));
        $this->display();
    }

    /**
     * ajax异步加载商品
     */
    public function ajax_index()
    {
        $start = I('post.start');
        $list = $this->product->limit($start, 2)->select();
        $this->ajaxReturn(array('result' => $list, 'status' => 1, 'msg' => '获取成功！'));
    }

    /**
     * 分类首页
     */
    public function category()
    {
        $categories = $this->category->where('parent_id != 0')->select();
        foreach ($categories as &$category) {
            $category['children'] = $this->product->where("category_id = '$category[id]' and status = 1")->select();
        }

        $this->assign(compact('categories'));
        $this->display();
    }

    /**
     * 商品显示首页
     */
    public function show()
    {
        $id = I("get.id");
        $products = $this->product->where("id = '$id'")->find();
        //查出商品对应的相册
        $galleries = $this->gallery->where("product_id = '$id'")->limit(3)->select();
        $this->assign(compact('products', 'galleries'));
        $this->display();
    }

}

